package vip.xiaonuo.bi.modular.dataset.enums;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.SneakyThrows;
import vip.xiaonuo.bi.modular.dataset.param.DataSetCsvTypeParam;
import vip.xiaonuo.bi.modular.dataset.param.DataSetSqlTypeParam;
import vip.xiaonuo.bi.modular.dataset.param.DataSetTypeParam;
import vip.xiaonuo.bi.modular.datasource.dto.Colunm;

/**
 * @auther: 顾育司
 * @Description 具体参数处理
 * @date: 2024/1/13
 */
public enum DatasetType {
    SQL("sql", DataSetSqlTypeParam.class),
    FILE_CSV("file_csv", DataSetCsvTypeParam.class),

    // xlsx 和 xls 两种类型的文件都归属
    FILE_EXCEL("file_excel", DataSetSqlTypeParam.class),

    ;

    private String type;
    private Class<? extends DataSetTypeParam> paramClass;
    DatasetType(String type, Class<? extends DataSetTypeParam> paramClass){
        this.type= type;
        this.paramClass= paramClass;
    }

    /**
     * 把json参数解析成具体参数
     * @return
     */
    @SneakyThrows
    public static DataSetTypeParam parseParam(String type, String dataJson){
        DataSetTypeParam dataSetTypeParam = JSONObject.parseObject(JSONObject.parseObject(dataJson).toString(), valueOf(type.toUpperCase()).paramClass);
        dataSetTypeParam.setColunms( JSONArray.parseArray(JSONObject.parseObject(dataJson).get("colums").toString(),Colunm.class));
        dataSetTypeParam.setColInfoType( JSONObject.parseObject(dataJson).get("colums").toString());
        return dataSetTypeParam;
    }


}
